package com.emptyMe.org.controller;


import com.emptyMe.basic.utils.AjaxResult;
import com.emptyMe.org.domain.Goods;
import com.emptyMe.org.domain.Size;
import com.emptyMe.org.domain.Type;
import com.emptyMe.org.query.GoodsQuery;
import com.emptyMe.org.service.IGoodsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@Api(tags = "部门接口",description = "商品接口详细描述")
public class GoodsController {

    @Resource
    private IGoodsService service;

    @PutMapping
    @ApiOperation(value = "添加或修改商品",notes = "如果有id就修改，没有就添加")
    public AjaxResult addOrUpdate(@RequestBody Goods goods){
        try {
            service.SaveGood(goods);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"更新或添加失败"+e.getCause());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除商品")
    public AjaxResult del(@PathVariable List<Long> id){
        try {
            service.DelGood(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }

    @DeleteMapping("/delMany")
//    @ApiOperation(value = "批量删除")
    public AjaxResult delAll(@RequestBody List<Goods> goods){
        try {
            ArrayList<Long> longs = new ArrayList<>();
            for (Goods good : goods) {
                longs.add(good.getId());
            }
            service.DelGood(longs);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult findAll(@RequestBody GoodsQuery query){
        PageInfo<Goods> info = service.findAllGood(query);
        return AjaxResult.me(info);
    }

    @PostMapping("/type")
    public AjaxResult findType(){
        List<Type> type = service.findType();
        return AjaxResult.me(type);
    }

    @PostMapping("/size")
    public AjaxResult findSize(@RequestBody Map<String,Long> id){
        System.out.println(id);
        List<Size> size = service.findSize(id.get("id"));
        return AjaxResult.me(size);
    }


    @PostMapping("/sum")
    public AjaxResult sumNum(@RequestBody Map<String,Long> id){
        Integer id1 = service.sumNum(id.get("id"));
        return AjaxResult.me(id1);
    }
}
